<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间" prop="roleKey">
        <el-input
          v-model="queryParams.roleKey"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预定人" prop="reservationName">
        <el-input
          v-model="queryParams.reservationName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单来源" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="订单来源"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新建</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-radio-group v-model="current" @input="stateHandel">
        <el-radio-button :label="index" v-for="(item, index) in countHouseList" :key="index"
          >{{ item.label }}（{{ item.number }}）</el-radio-button
        >
      </el-radio-group>
    </el-row>
    <div class="info">
      <i class="el-icon-info"></i> <span>已选择</span> {{ ids.length }}
      <span>项</span> <label @click="clearSelect" class="clear">清空</label>
    </div>

    <el-table
      v-loading="loading"
      :data="roomList"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" prop="orderId" width="120" />
      <el-table-column
        label="房间"
        prop="roomNumber"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="预定人信息"
        prop="reservationName"
        :show-overflow-tooltip="true"
        width="150"
      >
      <template slot-scope="scope">
         {{ scope.row.reservationName }}-
         {{ scope.row.reservationPhone }}
        </template>
    </el-table-column>
      <el-table-column label="来源" prop="houseType" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dict_house_type"
            :value="scope.row.houseType"
          />
        </template>
      </el-table-column>
      <el-table-column label="租金（元）" prop="roleSort" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dict_online_rental_type"
            :value="scope.row.onlineRentalType"
          />
        </template>
      </el-table-column>
      <el-table-column label="押金（元）" prop="roleSort"  />
      <el-table-column label="支付总额（元）" prop="roleSort"  />
      <el-table-column label="状态" prop="houseManagerName"  />
      <el-table-column label="预计时长（天）" prop="houseManagerName"  />
      <el-table-column label="预计入住时间" prop="houseManagerName"  />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleChangeHouse(scope.row)"
            >换房</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >续租</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >退租</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >取消</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >办理入住</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 详情弹窗 -->
    <order-detail
      :open.sync="detailDialogVisible"
      v-if="detailDialogVisible"
      :orderId="orderId"
    ></order-detail>
    <!-- 换房 -->
    <change-house :visible="openChangeHouse" :orderInfo="orderInfo" @closeeldialog="dialogClose"></change-house>
    <!-- 编辑 -->
    <order-edit :visible="openEdit" :orderInfo="orderInfo" @closeeldialog="dialogClose"></order-edit>
  </div>
</template>

<script>
import {
  listOrder,
  countOrderState,
} from "@/api/order/order";
import orderDetail from "./component/order-detail.vue";
import changeHouse from "./component/change-house.vue";
import orderEdit from "./component/order-edit.vue";
export default {
  name: "orderList",
  dicts: ["dict_house_nature", "dict_house_type", "dict_online_rental_type"],
  components: {
    orderDetail,
    changeHouse,
    orderEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前选择
      current: "0",
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],

      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomNumber: undefined,
        roleKey: undefined,
        status: undefined,
        orderState: undefined,
        orderIds:undefined
      },
      countHouseList: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        roomNumber: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" },
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" },
        ],
      },    
      detailDialogVisible: false,
      orderId: "",
      openChangeHouse:false,
      openEdit:false,
      orderInfo:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dialogClose(){
      this.openChangeHouse=false;
      this.openEdit=false;
    },
    stateHandel(i) {
      let orderState = this.countHouseList[i].value;
      this.queryParams.orderState = orderState;
      this.handleQuery();
    },
    diaclose() {
      this.open = false;
      this.getList();
    },
    /** 查询房间列表 */
    getList() {
      this.loading = true;
      listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.roomList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
      countOrderState().then((response) => {
        this.countHouseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.queryParams.orderIds=this.ids
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.orderId='';
      this.open = true;
    },
    // 详情操作
    handleDetail(row) {
      this.orderId = row.orderId;
      this.detailDialogVisible = true;
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.orderInfo = row;
      this.openEdit = true;
    },
    //换房操作
    handleChangeHouse(row){
      this.orderInfo = row;
      this.openChangeHouse = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "enterprise/order/order/export",
        {
          ...this.queryParams,
        },
        `订单_${new Date().getTime()}.xlsx`
      );
    },
    //请空选择项
    clearSelect() {
      this.$refs.table.clearSelection();
    },
  },
};
</script>
<style lang="scss" scoped>
.title {
  font-size: 14px;
  font-weight: 500;
  color: #666;
}
.total {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  margin-top: 6px;
}
.left-border {
  border-left: 1px solid #999;
}
.right-border {
  border-right: 1px solid #999;
}
.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;
  span {
    color: #565c5f;
  }
  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}
</style>
